package com.imooc.user.service;

import com.imooc.thrift.user.UserInfo;
import com.imooc.thrift.user.UserService;
import com.imooc.user.dao.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * @Auther: zhaozeyang
 * @Date: 2018/8/18 17:45
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService.Iface {


    @Autowired
    private UserRepository userRepository;

    @Override
    public UserInfo getUserById(int id) {
        UserInfo userInfo = new UserInfo();
        com.imooc.thrift.user.domin.UserInfo userInfo1 = userRepository.findById(Long.valueOf(id));
        BeanUtils.copyProperties(userInfo1, userInfo);
        return userInfo;
    }

    @Override
    public UserInfo getUserByName(String username) {
        UserInfo userInfo = new UserInfo();
        com.imooc.thrift.user.domin.UserInfo userInfo1 = userRepository.findOneByUsername(username);
        BeanUtils.copyProperties(userInfo1, userInfo);
        return userInfo;
    }

    @Override
    public void regiserUser(UserInfo userInfo) {
        com.imooc.thrift.user.domin.UserInfo userInfo1 = new com.imooc.thrift.user.domin.UserInfo();
        BeanUtils.copyProperties(userInfo, userInfo1);
        if (userInfo1.getId().intValue() == 0) {
            userInfo1.setId(null);
        }
        com.imooc.thrift.user.domin.UserInfo result = userRepository.save(userInfo1);
        System.out.println("save success " + result.getId().toString());
    }


    //@PostConstruct
    public void hello() {
        com.imooc.thrift.user.domin.UserInfo userInfo = new com.imooc.thrift.user.domin.UserInfo();
        userInfo.setEmail("709796558@qq.com");
        userInfo.setMobile("18271396325");
        userInfo.setPassword("1231");
        userInfo.setRealName("zhaozeyang");
        userInfo.setUsername("hahah");
        userInfo.setId(1);
        com.imooc.thrift.user.domin.UserInfo result = userRepository.save(userInfo);

    }
}
